/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
/*
 * tameshi
 * Copyright (C) Massimiliano Maniscalco 2011, 2012 <massi.neptune@yahoo.com>
 * 
tameshi is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * tameshi is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "test_sceltamult_domande.h"

void TestSceltaMultDomande::
run_test ( void )
{
	int nr = _contenuto->size();
	_domandeTotLabel->set_text ( Glib::ustring::format ( nr ) );
	_testWin->present ();
	_timer = 0;
	_nrDom = 0;
	update_window ();
}

void TestSceltaMultDomande::
on_edit_clicked ( void )
{
	commit_domanda_edit.emit ( *_domandaCorrente );
	_timerConnection.disconnect ();
	next_question ();
	pause_test ();
}

void TestSceltaMultDomande::
update_window ( void )
{
	_domandaNumLabel->set_text ( Glib::ustring::format ( _nrDom + 1 ) );
	_timerConnection = Glib::signal_timeout ().connect_seconds (sigc::mem_fun (*this, &TestSceltaMultDomande::on_timer_fired), 1 );
	_tempoLabel->set_text ( "00 : 00" );
	_testWin->set_title ( _domandaCorrente->get_codice () );
	_domandaCorrente->get_domanda ( _domandaTxtBfr );
	_domandaCorrente->get_risposta ( 0, _rispostaATxtBfr );
	_domandaCorrente->get_risposta ( 1, _rispostaBTxtBfr );
	_domandaCorrente->get_risposta ( 2, _rispostaCTxtBfr );
	_domandaCorrente->get_risposta ( 3, _rispostaDTxtBfr );
	_rispostaBuona = _domandaCorrente->get_corretta ();
	_rispostaEsatta = _domandaCorrente->get_risposta ( _rispostaBuona );
}

void TestSceltaMultDomande::
on_risposta ( char tasto )
{
	bool risultato = ( tasto == _rispostaBuona );
	_timerConnection.disconnect ();
	_domandaCorrente->test ( risultato, _timer );
	commit_domande_changes.emit ( *_domandaCorrente );
	if ( !risultato )
	{
		Glib::ustring messaggio = "Sbagliato, la risposta giusta è\n<b><big>" + _rispostaEsatta + "</big></b>";
		Gtk::MessageDialog *_alertDialog = new Gtk::MessageDialog ( *_testWin, "", false, Gtk::MESSAGE_WARNING, Gtk::BUTTONS_OK, true);
		_alertDialog->set_message ( messaggio, true);
		int res_id = _alertDialog->run();
		delete _alertDialog;
	}
	next_question ();
}

void TestSceltaMultDomande::
next_question ( void )
{
	_timer = 0;
	_nrDom++;
	_domandaCorrente++;
	if ( _domandaCorrente == _contenuto->end() )
		_testWin->hide();
	else
		update_window ();
}
